import { cancelTransportAPI, changeAdjustAPI, fetchTransportDetailAPI, fetchTransportListAPI, fetchTransportMapAPI, fetchTripsAPI } from '@/api/dispatch/transportAPI'

const actions = {
  /*
  * 获取运输任务分页数据的action
  *  */
  async fetchTransportListAction(context, pageParams) {
    const { data } = await fetchTransportListAPI(pageParams)
    context.commit('setTransportLisMutation', data)
  },
  /*
  * 取消运输任务的action
  *  */
  async cancelTransportAction(context, id) {
    await cancelTransportAPI(id)
    return '运输任务取消成功'
  },
  /*
  *
  * 手动调整车次的action
  *  */
  async changeAdjustAction(context, formData) {
    await changeAdjustAPI(formData)
    return '调整成功'
  },
  /*
  *
  * 获取运输任务详情的action
  *  */
  async fetchTransportDetailAction(context, id) {
    const { data } = await fetchTransportDetailAPI(id)
    context.commit('setTransportDetailMutation', data)

  },
  /*
  *
  * 获取运输任务坐标的action
  *  */
  async fetchTransportMapAction(context, id) {
    const { data } = await fetchTransportMapAPI(id)
    context.commit('setTransportMapMutation', data)

  },

  /*
  *
  * 获取车次列表的action
  *  */
  async fetchTripsAction(context,transportLineId){
    const { data } = await fetchTripsAPI(transportLineId)
    context.commit('setTripsMutation', data)
  }
}

const mutations = {
  /*
  * 设置运输任务分页数据的mutation
  *  */
  setTransportLisMutation(state, { counts, items }) {
    state.transportList = items
    state.counts = Number(counts)
  },
  /*
  *
  * 设置运输任务详情的mutation
  *  */
  setTransportDetailMutation(state, transportDetail) {
    state.transportDetail = transportDetail
  },

  /*
  *
  * 设置运输任务坐标的mutation
  *  */
  setTransportMapMutation(state, transportMap) {
    state.transportMap = transportMap
  },
  /*
  *
  *  */
  setTripsMutation(state,trips){
    state.trips = trips
  }
}
const state = () => {
  return {
    transportList: [],
    counts: 0,
    transportDetail: {},
    transportMap: [],
    trips:[]
  }
}

export default {
  actions,
  mutations,
  state,
  namespaced: true
}
